<template>
  <div class="container">
    <Save-Position ref="savePosition" @refresh="fetchData" />
    <a-card class="general-card" title="数据字典">
      <a-row class="grid-demo" :gutter="24">
        <a-col :span="8">
          <a-table :data="treeData" :show-header="false">
            <template #columns>
              <a-table-column title="名称" data-index="name" />
            </template>
          </a-table>
        </a-col>
        <a-col :span="16">
          <a-row>
            <a-col :flex="1">
              <a-form
                :model="formModel"
                :label-col-props="{ span: 18 }"
                :wrapper-col-props="{ span: 18 }"
                label-align="left"
              >
                <a-row :gutter="12">
                  <a-col :span="12">
                    <a-form-item field="keyWords">
                      <a-input-search
                        v-model="formModel.keywords"
                        placeholder="请输入关键字查询"
                        allow-search
                        allow-clear
                        @search="fetchData()"
                        @keydown.enter="fetchData"
                      />
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-form>
            </a-col>
            <a-col :flex="'86px'" style="text-align: right">
              <a-space>
                <a-button type="primary" @click="handleClickToSave()">
                  <template #icon>
                    <icon-plus />
                  </template>
                  {{ $t('searchTable.operation.create') }}
                </a-button>
              </a-space>
            </a-col>
          </a-row>
          <a-table
            row-key="id"
            :loading="loading"
            :data="renderData"
            column-resizable
            :hide-expand-button-on-empty="true"
            :bordered="false"
          >
            <template #columns>
              <a-table-column title="岗位编码" data-index="dictValue" />
              <a-table-column title="岗位名称" data-index="dictName" />
              <a-table-column title="岗位简称" data-index="simpleName" />
              <a-table-column title="岗位级别" data-index="levelName" />
              <a-table-column
                :title="$t('columns.operations')"
                align="right"
                data-index="operations"
                :width="180"
              >
                <template #cell="{ record }">
                  <a-button
                    v-if="record.sysInnerFlag == 0"
                    type="text"
                    size="small"
                    @click="handleClickToEdit(record)"
                  >
                    {{ $t('columns.operations.edit') }}
                  </a-button>
                  <a-popconfirm
                    v-if="record.sysInnerFlag == 0"
                    :content="$t('operations.popConfirm.delete')"
                    type="error"
                    @ok="handleClickToDel(record.id)"
                  >
                    <a-button type="text" size="small" status="danger">
                      {{ $t('columns.operations.delete') }}
                    </a-button>
                  </a-popconfirm>
                </template>
              </a-table-column>
            </template>
          </a-table>
        </a-col>
      </a-row>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import { Message } from '@arco-design/web-vue';
  import { OrgVo } from '@/api/organization';
  import {
    PositionVo,
    selectPositionByOrgId,
    delPosition,
  } from '@/api/position';
  import tree from '@/utils/tree';
  import SavePosition from './components/save-position.vue';

  const positionFormModel = () => {
    return {
      keywords: '',
      orgId: '',
    };
  };
  const { loading, setLoading } = useLoading();
  const formModel = ref(positionFormModel());
  const savePosition = ref();
  const renderData = ref<OrgVo[]>([]);
  const treeData = ref();
  const fetchData = async (params = { ...formModel.value }) => {
    setLoading(true);
    try {
      const { data } = await selectPositionByOrgId(
        params.keywords,
        params.orgId
      );
      renderData.value = tree.toTree(data, 'id', 'parentId');
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };
  const handleClickTreeNode = (node: any) => {
    formModel.value.orgId = node.id;
    if (formModel.value.orgId) {
      fetchData();
    }
  };

  const handleClickToSave = () => {
    savePosition.value.handleChangeVisible();
  };
  const handleClickToEdit = (obj: PositionVo) => {
    savePosition.value.handleChangeVisible(obj, 'edit');
  };

  const handleClickToDel = (id: string) => {
    delPosition(id).then(() => {
      Message.success('操作成功');
      fetchData();
    });
  };
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
